package controller;

import model.SocketMap;


import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    public static void main(String[] args) {
        new Server().strat();
    }

    public void strat() {
        ServerSocket serverSocket = null;

        try {
            serverSocket = new ServerSocket(6666);
            while (true) {
                Socket socket = serverSocket.accept();
                System.out.println("A client connected");
                ObjectInputStream is = new ObjectInputStream(socket.getInputStream());
                Integer id = (Integer) is.readObject();
                // id socket 配对 id是登录时的 socket是连接时候的socket
                //存储id socket 为后面线程提供socket
                SocketMap socketMap = new SocketMap();
                socketMap.addSocket(id, socket);
                TClient tc = new TClient(socket);
                Thread t = new Thread(tc);
                t.start();
            }
        } catch (BindException e) {
            System.out.println("服务器未关闭 请关闭后重新启动服务器");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}
